﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using SilverlightOracle.WCFService;
using System.Text.RegularExpressions;
namespace SilverlightOracle
{
    public partial class QuanTriTruong : Page
    {
        ServiceClient webservice = new ServiceClient();
        public QuanTriTruong()
        {
            InitializeComponent();
            
            dgView.Visibility = Visibility.Collapsed;
        }
        
        void ShowTruongCompleted(object sender, ShowTruongCompletedEventArgs e)
        {
            dgView.Visibility = Visibility.Visible;
            dgView.ItemsSource = e.Result;
        }
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void dgView_GotFocus(object sender, RoutedEventArgs e)
        {
            var cw = new SubQuanTriTruong();
            cw.SelectedUser = dgView.SelectedItem as detailtruong;
            cw.Show();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ServiceClient webservice1 = new ServiceClient();
            webservice.ShowAllTruongCompleted +=new EventHandler<ShowAllTruongCompletedEventArgs>(webservice_ShowAllTruongCompleted);
            webservice.ShowAllTruongAsync();
        }
        void webservice_ShowAllTruongCompleted(object sender, ShowAllTruongCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                dgView.Visibility = Visibility.Visible;
                dgView.ItemsSource = e.Result;
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate()) return;
            else
            {
                ServiceClient webservice = new ServiceClient();
                webservice.ShowTruongCompleted += new EventHandler<ShowTruongCompletedEventArgs>(webservice_ShowTruongCompleted);
                if (radioButton1.IsChecked == true)
                {
                    webservice.ShowTruongAsync(textBox1.Text, "", 0);
                }
                else
                {
                    if (radioButton2.IsChecked == true)
                    {
                        webservice.ShowTruongAsync("", textBox1.Text, 0);
                    }
                    else
                    {
                        webservice.ShowTruongAsync("", "", Convert.ToInt32(textBox1.Text));
                    }
                }
            }

        }
        void webservice_ShowTruongCompleted(object sender, ShowTruongCompletedEventArgs e)
        {

            if (e.Result.Count != 0)
            {
                label1.Content = "";
                dgView.Visibility = Visibility.Visible;
                dgView.ItemsSource = e.Result;
            }
            else
            {
                label1.Content = "Không tìm thấy trường . Yêu cầu kiểm tra lại từ khóa !";
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            var cw = new InsertTruong();
                cw.Show();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
           // radioButton2.IsChecked = true;
        }
        bool Validate()
        {

            if (textBox1.Text.Length == 0)
            {
                label1.Content = "Từ khóa rỗng";
                textBox1.Focus();
                return false;
            }
            if (radioButton1.IsChecked == true)
            {
                if (!Regex.IsMatch(textBox1.Text, @"^[a-zA-Z0-9''-'\s]{1,40}$"))
                {
                    label1.Content = "Mã trường chỉ gồm số và chữ không dấu";
                    textBox1.Focus();
                    return false;
                }
            }
            if (radioButton2.IsChecked == true)
            {
                if (!Regex.IsMatch(textBox1.Text, @"^[a-zA-Z''-'\s]{1,40}$"))
                {
                    label1.Content = "Tên trường phải là chữ";
                    textBox1.Focus();
                    return false;
                }
            }
            if (radioButton3.IsChecked == true)
            {
                int num1;
                bool IsNumberisDT = int.TryParse(textBox1.Text, out num1);
                if (!IsNumberisDT)
                {
                    label1.Content = "Mã người dùng chỉ có chữ ";
                    textBox1.Focus();
                    return false;
                }
            }
            return true;
        }

    }
}
